package com.shanjupay.merchant.controller;

import com.shanjupay.merchant.api.MerchantService;

import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.convert.MerchantRegisterConvert;
import com.shanjupay.merchant.service.SmsService;
import com.shanjupay.merchant.vo.MerchantRegisterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: xinglongge
 * @Date: 2020/4/11 16 00
 * @Description:
 */
@RestController
@Api(value = "商户平台应用接口")
@Slf4j
public class MerchantController {


    @Reference
    private MerchantService merchantService;

    @Autowired
    SmsService smsService;

    @GetMapping("/merchants/{id}")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long id) {

        return merchantService.queryMerchantById(id);
    }

    @ApiOperation("获取手机验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query")
    @GetMapping("/sms")
    public String getSMSCode(@RequestParam String phone) {
        String verifyCode = smsService.smsVerifyCode(phone);
        log.info("向手机号:{}发送验证码:{}", phone, verifyCode);
        return verifyCode;
    }

    @ApiOperation("商户注册")
    @ApiImplicitParam(name = "merchantRegisterVO", value = "商户注册信息", required = true, dataType = "MerchantRegisterVO", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVO registerMerchant(@RequestBody MerchantRegisterVO merchantRegisterVO) {
        //校验验证码
        smsService.checkVerifyCode(merchantRegisterVO.getVerifiykey(), merchantRegisterVO.getVerifiyCode());
        //调用dubbo服务接口
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2dto(merchantRegisterVO);
        // MerchantDTO merchantDTO = new MerchantDTO();
        //  BeanUtils.copyProperties(merchantRegisterVO, merchantDTO);
        merchantService.createMerchant(merchantDTO);
        return merchantRegisterVO;
    }

}
